package cn.b.sky.admin.security.context;

import org.springframework.util.Assert;

/**
 * 
 * <strong>Title : GlobalManagementContextHolderStrategy.java<br></strong>
 * <strong>Package : cn.b.sky.admin.security.context<br></strong>
 * <strong>Description : </strong>全局缓存，和线程无关<br> 
 * <strong>Create on : 2016-12-19 上午10:34:23<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class GlobalManagementContextHolderStrategy implements ManagementContextHolderStrategy {

  private static ManagementContext contextHolder;

  @Override
  public void clearContext() {
    contextHolder = null;
  }

  @Override
  public ManagementContext getContext() {
    if (contextHolder == null) {
      contextHolder = new ManagementContextImpl();
    }

    return contextHolder;
  }

  @Override
  public void setContext(ManagementContext context) {
    Assert.notNull(context, "Only non-null SecurityContext instances are permitted");
    contextHolder = context;
  }

  @Override
  public ManagementContext createEmptyContext() {
    return new ManagementContextImpl();
  }

}
